package com.hxd.otc.module.home.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hxd.otc.R;
import com.hxd.otc.base.BaseAdapter;
import com.hyphenate.helpdesk.domain.TicketEntity;
import com.hyphenate.helpdesk.util.ISO8601DateFormat;
import com.hyphenate.util.DateUtils;

import java.text.ParseException;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by liyuzhao on 16/9/8.
 */
public class TicketListAdapter extends BaseAdapter<TicketListAdapter.TicketListHolder, TicketEntity> {

    private ISO8601DateFormat mDateFormat = new ISO8601DateFormat();

    public TicketListAdapter(Context context) {
        super(context);
    }

    @Override
    protected void onBindHolder(@NonNull TicketListHolder holder, int position) {
        TicketEntity item = getItem(position);
        holder.tvName.setText(String.format("%s %s", mContext.getResources().getString(R.string.leave_theme), item.getSubject()));
        try {
            holder.tvTime.setText(DateUtils.getTimestampString(mDateFormat.parse(item.getUpdated_at())));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String content = item.getContent();
        holder.tvContent.setText(content/*.substring(0,contentLastIndex)*/);
    }

    @NonNull
    @Override
    public TicketListHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new TicketListHolder(mInflater.inflate(R.layout.item_icket, parent, false));
    }

    static class TicketListHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_date)
        TextView tvTime;
        @BindView(R.id.tv_content)
        TextView tvContent;

        TicketListHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

}
